package com.kalix.vote.system.dict.biz;

import com.kalix.vote.system.dict.api.biz.IVoteDictBeanService;
import com.kalix.vote.system.dict.api.dao.IVoteDictBeanDao;
import com.kalix.vote.system.dict.entities.VoteDictBean;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.system.BaseDictServiceImpl;

import java.util.ArrayList;
import java.util.List;

public class VoteDictBeanServiceImpl extends BaseDictServiceImpl<IVoteDictBeanDao, VoteDictBean> implements IVoteDictBeanService {

    /**
    @Override
    public JsonStatus saveEntity(ExamDictBean entity) {
        Integer maxValue = dao.getFieldMaxValue("value", "type='" + entity.getType() + "'");

        maxValue = maxValue + 1;

        entity.setValue(maxValue);

        return super.saveEntity(entity);
    }
**/
    @Override
    public VoteDictBean getDictBeanByTypeAndValue(String type, String value) {
        VoteDictBean voteDictBean = new VoteDictBean();
        String tbName = dao.getTableName();
        String sql = "select * from %s where type='%s' and value='%s'";
        if (tbName != null) {
            sql = String.format(sql, tbName, type, value);
            List list = dao.findByNativeSql(sql, VoteDictBean.class);
            if (list.size() == 1) {
                voteDictBean = (VoteDictBean) list.get(0);
            }
        }
        return voteDictBean;
    }

    @Override
    public List<VoteDictBean> getDictBeanByType(String type) {
        List<VoteDictBean> list = new ArrayList<>();
        String tbName = dao.getTableName();
        String sql = "select * from %s where type='%s'";
        if (tbName != null) {
            sql = String.format(sql, tbName, type);
            list = dao.findByNativeSql(sql, VoteDictBean.class);
        }
        return list;
    }
}
